<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ActivityAction
 *
 * @author guochen
 */

namespace Admin\Controller;

use Think\Controller;

class ActivityController extends CommonController {

    function votelog() {
        header("Content-type: text/html; charset=utf-8");
        $openTime = array(
            1 => array("00:00:00-00:59:59"),
            2 => array("01:00:00-01:59:59"),
            3 => array("02:00:00-02:59:59"),
            4 => array("03:00:00-03:59:59"),
            5 => array("04:00:00-04:59:59"),
            6 => array("05:00:00-05:59:59"),
            7 => array("06:00:00-06:59:59"),
            8 => array("07:00:00-07:59:59"),
            9 => array("08:00:00-08:59:59"),
            10 => array("09:00:00-09:59:59"),
            11 => array("10:00:00-10:59:59"),
            12 => array("11:00:00-11:59:59"),
            13 => array("12:00:00-12:59:59"),
            14 => array("13:00:00-13:59:59"),
            15 => array("14:00:00-14:59:59"),
            16 => array("15:00:00-15:59:59"),
            17 => array("16:00:00-16:59:59"),
            18 => array("17:00:00-17:59:59"),
            19 => array("18:00:00-18:59:59"),
            20 => array("19:00:00-19:59:59"),
            21 => array("20:00:00-20:59:59"),
            22 => array("21:00:00-21:59:59"),
            23 => array("22:00:00-22:59:59"),
            24 => array("23:00:00-23:59:59"),
        );
//        for($i=0;$i<=24;$i++){
//
//            $str1 = $i>=10 ? $i.':00:00' : '0'.$i.':00:00';
//            $str2 = $i>=10 ? $i.':59:59' : '0'.$i.':59:59';
//            echo ($i+1).'=>array("'.$str1.'-'.$str2.'"),'.'<br>';
//        }
//        exit;
        //print_r($openTime);
        // echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 98));exit;
        $where = array();
        $listArr = array();
        $s = 0;
        $vote = M('Vote_recode');
        for ($i = 14; $i <= date('d'); $i++) {
            $dateArr[] = date('Y-m-') . $i;
            echo "<p style='color:red'>" . date('Y-m-') . $i . '</p>';
            foreach ($openTime as $k => $v) {
                $listArr[$k] = $v[0];

                $arr = explode('-', $v[0]);
                // echo $arr[0].'<br>';
                $time1 = strtotime(date('Y-m-') . $i . $arr[0]);
                $time2 = strtotime(date('Y-m-') . $i . $arr[1]);
                if ($_REQUEST['userid'])
                    $map['user_id'] = $_REQUEST['userid'];
                $map['crttime'] = array('between', array($time1, $time2));
                // $where['crttime'] = array('like','%'.$time.'%');
                $count = $vote->where($map)->count();
                echo "times:" . $v[0] . "&nbsp;&nbsp;count:" . $count;
                // $cArr[][$v[0]] =$count;
                // echo $vote->getLastSql();
                $s+=$count;
                echo '<br>';
            }

            echo '<br>截止' . date('Y-m-') . $i . '总票数:' . $s;
        }
//        foreach ($openTime as $k=>$v){
//             $listArr[$k] = $v[0];
//        }
//        $this->assign('list',$listArr);
//       // print_r($cArr);exit;
//        $this->assign('data',$dateArr);
//        $this->display();
    }

    //活动列表
    public function index() {
        if (!empty($_REQUEST['keys'])) {
            $map['title'] = array(
                'like',
                '%' . $_REQUEST['keys'] . '%'
            );
        }
        $map['status'] = 1;
        $gc = M('Activity');

        $count = $gc->where($map)->count(); // 查询满足要求的总记录数

        $Page = new \ Think \ Page($count, C('PAGE_SIZE')); // 实例化分页类 传入总记录数
        $nowPage = !empty($_REQUEST['p']) ? $_REQUEST['p'] : 1;
        foreach ($map as $key => $val) {
            $Page->parameter .= "$key=" . urlencode($val) . "&";
        }
        $show = $Page->show(); // 分页显示输出
        $data = $gc->where($map)->order('crttime desc')->page($nowPage . ',' . $Page->listRows)->select();
        $this->assign('list', $data);
        $this->assign('page', $show);
        $this->display();
    }

    function loadlist() {
        if (!empty($_REQUEST['keys'])) {
            $map['title'] = array(
                'like',
                '%' . $_REQUEST['keys'] . '%'
            );
        }
        $map['status'] = 1;
        $data = M('Activity')->where($map)->select();
        $count = M('Activity')->where($map)->count();
        if ($data) {
            $str = '"success": true, "totalRows": ' . $count . ', "curPage": 1, "data"';
            foreach ($data as $k => $v) {
                $data[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
                $data[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
                $data[$k]['number'] = M('User')->where(array('activity_id' => $v['id'], 'state' => 2))->count();
                $menjoin1 = M('Vote_recode')->alias("r")
                        ->join("join v_user as u on r.openid=u.open_id ")->where('r.activity_id=' . $v['id'])
                        ->count();
               // $menjoin2 = M('User')->where(array('activity_id' => $v['id'], 'state' => 2))->count();

                $data[$k]['memberjoin'] = $menjoin1 ;
            }
            $a = $this->Export($data, $str);
        } else {

            $str = '"success": false, "totalRows": ' . $count . ', "curPage": 1, "data"';
            $a = $this->Export($data, $str);
        }
        echo $a;
    }

    //创建活动
    public function addActivity() {
        $map['id'] = $_REQUEST['id'];
        $gc = M('Activity');
        $data = $gc->where($map)->find();
        if (empty($data)) {
            $data['rules'] = '';
            $data['p_stint'] = $data['t_stint'] = 1;
            $data['start_time'] = $data['end_time'] = time();
        }

        $this->assign('data', $data);

        $this->display();
    }

    //保存创建活动
    public function saveAddAvtivity() {
        $id = $_POST['id'];
        $data['title'] = $_POST['title'];
        $data['p_stint'] = $_POST['p_stint'];
        $data['t_stint'] = $_POST['t_stint'];
        $data['rules'] = nl2br($_POST['rules']);
        $data['lottery'] = nl2br($_POST['lottery']);
        $data['start_time'] = strtotime($_POST['start_time']);
        $data['end_time'] = strtotime($_POST['end_time']);
        $data['getian'] = $_POST['getian'] ? $_POST['getian'] : 0;
        $data['repeat'] = $_POST['repeat'] ? $_POST['repeat'] : 0;
        $imgPath = $this->upload();

        if ($imgPath) {
            $data['imgurl'] = $imgPath;
        }
        //  print_r($data);exit;
        $gc = M('Activity');
        if (empty($id)) {
            $data['uptime'] = $data['crttime'] = time();
            if ($gc->add($data)) {

                $this->success('数据已保存！', 'index');
            } else {
                $this->error('数据未保存！');
            }
        } else {
            $data['uptime'] = time();
            $map['id'] = $id;
            //print_r($data);exit;
            if ($gc->where($map)->save($data)) {
                //echo $gc->getLastSql();exit;
                // echo $gc->getLastSql();exit;
                $this->success('数据已保存！', 'index');
            } else {
                $this->error('数据未保存！');
            }
        }
    }

    //删除活动
    function delActivity() {
        $map['id'] = $_REQUEST['id'];
        $gc = M('Activity');
        $state = $gc->where($map)->setField('status', 3);
        $gc->where($map)->setField('uptime', time());
        if (!empty($state)) {
            $this->success('数据删除成功！', 'index');
        } else {
            $this->error('数据删除失败！');
        }
    }

    //人员
    public function userList() {
        if (!empty($_REQUEST['keys'])) {
            $map['name'] = array(
                'like',
                '%' . $_REQUEST['keys'] . '%'
            );
        }
       
        $map['activity_id'] = array(
            'eq',
            $_GET['id']
        );

        $User = M('User'); // 实例化User对象
        //  $count = $User->where($map)->count(); // 查询满足要求的总记录数
        // $Page = new \ Think \ Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $p = getpage($User, $map, 10);
        foreach ($map as $key => $val) {
            $Page->parameter .= "$key=" . urlencode($val) . "&";
        }
        //$Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        // $show = $Page->show(); // 分页显示输出
        // echo $show;
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        //$list = $User->where($map)->order('vote_num desc,gorder desc, id asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $list = $User->where($map)->order('vote_num desc,gorder desc, id asc')->select();

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show()); // 赋值分页输出

        $this->display();
    }

    //设置人员顺序
    public function setUserOrder() {

        $map['id'] = $_GET['id'];
        $gc = M('User');
        $state = $gc->where($map)->setField('gorder', $_GET['gorder']);
        $this->ajaxReturn($state, "OK！", 1);
    }

    //通过审核
    public function agree() {
        $map['id'] = $_GET['id'];
        $map['activity_id'] = $_GET['activityId'];
        $gc = M('User');
        $state = $gc->where($map)->save(array('state' => 2));
        if (!empty($state)) {
            $this->success('审核通过！', 'userList?id=' . $_GET['activityId']);
        } else {
            $this->error('审核失败！');
        }
    }

    //删除人员
    public function delUser() {
        $map['id'] = $_GET['id'];
        $map['activity_id'] = $_GET['activityId'];
        $gc = M('User');
        $state = $gc->where($map)->delete();
        $v = M('Vote_recode');
        $where['user_id'] = $_GET['id'];
        $state2 = $v->where($map)->delete();
        if (!empty($state)) {
            $this->success('数据删除成功！', 'userList?id=' . $_GET['activityId']);
        } else {
            $this->error('数据删除失败！');
        }
    }

    //新增人员
    public function addUser() {
        $activityId = $_GET['activityId'];
        $map['id'] = $_REQUEST['id'];
        $gc = M('User');
        $data = $gc->where($map)->find();

        $this->assign('data', $data);
        $this->assign('activityId', $activityId);
        $this->display();
    }

    //保存人员
    public function saveAddUser() {
        $id = $_POST['id'];
        $activityId = $_REQUEST['activityId'];

        $data['name'] = $_POST['name'];
        $imgPath = $this->upload();
        if ($imgPath) {
            $data['img'] = $imgPath;
        }
        $data['u_type'] = $_POST['u_type'];
        $data['info'] = nl2br($_POST['info']);
        $data['detail'] = nl2br($_POST['detail']);
        $data['activity_id'] = $activityId;
        $gc = M('User');
        if (empty($id)) {
            $data['uptime'] = $data['crttime'] = time();
            if ($gc->add($data)) {
                $this->success('数据已保存！', "userList?id=$activityId");
            } else {
                $this->error('数据未保存！');
            }
        } else {
            $data['uptime'] = time();
            $map['id'] = $id;
            $map['activity_id'] = $activityId;
            if ($gc->where($map)->save($data)) {
                $this->success('数据已保存！', "userList?id=$activityId");
            } else {
                $this->error('数据未保存！');
            }
        }
    }

    public function vote_log() {

        $vote = M('Vote_recode'); // 实例化User对象
        $map['user_id'] = $_REQUEST['id'];
        //  $count = $User->where($map)->count(); // 查询满足要求的总记录数
        // $Page = new \ Think \ Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $p = getpage($vote, $map, 20);
        foreach ($map as $key => $val) {
            $Page->parameter .= "$key=" . urlencode($val) . "&";
        }
        //$Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        // $show = $Page->show(); // 分页显示输出
        // echo $show;
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        //$list = $User->where($map)->order('vote_num desc,gorder desc, id asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $list = $vote->where($map)->order('crttime desc')->select();
        foreach ($list as $k => $v) {
            if (check_vip($v['openid'])) {
                $list[$k]['ishuiyuan'] = '是';
            } else {
                $list[$k]['ishuiyuan'] = '否';
            }
        }

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show()); // 赋值分页输出

        $this->display();
    }


    private function upload1() {

        import("ORG.Net.UploadFile");

        $upload = new UploadFile(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->allowExts = array(
            'jpg',
            'gif',
            'png',
            'jpeg'
        ); // 设置附件上传类型
        $upload->savePath = './upload/'; // 设置附件上传目录
        $upload->saveRule = uniqid("header_");

        if (!$upload->upload()) { // 上传错误提示错误信息
            //            $this->error($upload->getErrorMsg());
            $rs = FALSE;
        } else { // 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();
            $rs = $upload->savePath . $info[0]["savename"];
        }
        return $rs;
    }

}

?>
